package com.myapplication.ui.listfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.myapplication.R;
import com.myapplication.bean.ListBean;
import com.myapplication.config.HUrlConfig;
import com.myapplication.ui.listfragment.shoplistactivity.ShopListActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zitaojiang on 2016/10/6.
 */
public class Fragment_List extends Fragment implements Contract_List.View{
    private Contract_List.Presenter mPresenter;
    private PullToRefreshListView lv_list;
    private ListView mListview;
    private ListFragmentAdapter adapter;
    private Map<String,String> params=new HashMap<>();
    private List<ListBean.DataBean> listBeanList=new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter=new Presenter_List(this);
        params.put(HUrlConfig.key.CATEGORY,HUrlConfig.DefaultValue.CATEGORY);
        params.put(HUrlConfig.key.WITH_USER, HUrlConfig.DefaultValue.WITH_USER);
        params.put(HUrlConfig.key.WITH_MERCHANT, HUrlConfig.DefaultValue.WITH_MERCHANT);
        params.put(HUrlConfig.key.SINCE_TIME,HUrlConfig.DefaultValue.SINCE_TIME);
        params.put(HUrlConfig.key.MAX_TIME,HUrlConfig.DefaultValue.MAX_TIME);
        params.put(HUrlConfig.key.APP_VERSION,HUrlConfig.DefaultValue.APP_VERSION);
        params.put(HUrlConfig.key.PLATFORM,HUrlConfig.DefaultValue.PLATFORM);
        mPresenter.GetData(params);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //设置点击事件
        lv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //设置页面的跳转
                Intent intent=new Intent(getActivity(), ShopListActivity.class);
                //将所需数据传递到下一个页面
                intent.putExtra("comment",listBeanList.get(i-1).getComment());
                intent.putExtra("nickname",listBeanList.get(i-1).getUser().getNickname());
                intent.putExtra("comments_count",listBeanList.get(i-1).getCommentsCount());
                intent.putExtra("list_id",listBeanList.get(i-1).getContentId());
//                Log.i("TAG","======IDIDIDIDID======="+listBeanList.get(i-1).getContentId());
//                if(i==listBeanList.size()){
//                    intent.putExtra("title",listBeanList.get(i).getTitle().substring(6,17));
//                }else{
                    intent.putExtra("title",listBeanList.get(i-1).getTitle().substring(6,listBeanList.get(i-1).getTitle().length()));


                startActivity(intent);
            }
        });

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_list,null);
        lv_list= (PullToRefreshListView) view.findViewById(R.id.lv_qingdan);
//        mListview = (ListView) view.findViewById(R.id.lv);
//        Log.i("TAG11111", "onCreateView: mlistview");
        mListview=lv_list.getRefreshableView();
        lv_list.setMode(PullToRefreshBase.Mode.BOTH);
        //设置刷新
        initEvent();
        return view;
    }

    //上下拉刷新
    private void initEvent() {
        lv_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mPresenter.GetData(params);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
    }


    //成功获取数据后适配数据
    @Override
    public void onGetDataSuccess(final List<ListBean.DataBean> listBeanList) {
        this.listBeanList=listBeanList;
//        Log.i("tag", "onGetDataSuccess: "+listBeanList.size());
//        this.listBeanList.addAll(listBeanList);
        adapter=new ListFragmentAdapter(getActivity(),listBeanList);
//       adapter.setListFragmentAdapter(this.listBeanList);
        mListview.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        lv_list.onRefreshComplete();
    }

    @Override
    public void onGetDataFail(String error) {
        Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();//失败后提醒
    }
}
